package com.gwc.listeners;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;

@WebListener
public class ApplicationListener implements ServletContextListener, ServletContextAttributeListener {
    // 监听初始化
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        System.out.println("application:"+servletContext.hashCode()+"被初始化了");
    }
    // 监听销毁
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        System.out.println("application:"+servletContext.hashCode()+"被销毁了");
    }

    // 监听数据增加
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        ServletContext servletContext = scae.getServletContext();
        String name = scae.getName();
        Object value = scae.getValue();
        System.out.println("application:"+servletContext.hashCode()+"增加了("+name+","+value+")");
    }

    // 监听数据移除
    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        ServletContext servletContext = scae.getServletContext();
        String name = scae.getName();
        Object value = scae.getValue();
        Object newValue = servletContext.getAttribute(name);
        System.out.println("application:"+servletContext.hashCode()+
                "修改("+name+","+value+")为("+name+","+newValue+")");
    }

    // 监听数据修改

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        ServletContext servletContext = scae.getServletContext();
        String name = scae.getName();
        Object value = scae.getValue();
        System.out.println("application:"+servletContext.hashCode()+"移除了("+name+","+value+")");
    }
}
